package br.com.felix.clearcase.command;

import java.io.File;

import br.com.felix.easyclearcase.EasyClearCaseFile;
import br.com.felix.easyclearcase.EasyClearCaseFile.ClearCaseStatus;

/**
 * Base class for cleartool commands.
 * Cleartool commands must be executed on the runtime environment and diffent commands have
 * different parameters. 
 * This class provides a common interface for all commands, so that at the time to execute the commands,
 * we can abstract from the command's parameters.
 * */
public abstract class ClearToolCommand {

	/**
	 * The file that is target of the command (probably a file in the vob).
	 * */
	protected EasyClearCaseFile file;
	
	/**
	 * If the command needs to be performed in a certain directory, this attribute specifies it.
	 * If the command is indifferent to working directory, this can be null. For example: commands that use absolute paths in its syntax.
	 * */
	protected File workingDirectory = null;
	
	
	public File getWorkingDirectory() {
		return workingDirectory;
	}

	
	/**
	 * Defines in which directory the command will be executed.
	 * @param workingDirectory is the directory where the cleartool command will be executed.
	 * */
	public void setWorkingDirectory(File workingDirectory) {
		this.workingDirectory = workingDirectory;
	}

	/**
	 * Returns the cleartool command that implements the desired functionality of the concrete class.
	 * The returned String must be the entire command, hence, it must begin by the word "cleartool ...".
	 * @return a cleartool command that can be executed on the runtime environment.
	 * */
	public abstract String toCommandLine();

	
	
	public File getFile() {
		return file.getFile();
	}

	
	/**
	 * Defines the file that is target of the operation.
	 * @param file is a file in the vob that will be target of the clearcase operation.
	 * */
	public void setFile(EasyClearCaseFile file) {
		this.file = file;
	}

	
	/**
	 * Returns the file name of the file that is target of this command.
	 * */
	@Override
	public String toString() {
		return file.getName();
	}
	
	public abstract String getCommandName();
	
	/**
	 * Returns the status of the file if the command is successfully completed.
	 * Returns null if command does not modify the file's status.
	 * */
	protected abstract ClearCaseStatus getStatusAfterExecution();


	public void commandExecutedSuccessfully() {
		ClearCaseStatus newStatus = getStatusAfterExecution();
		if(newStatus!=null){
			file.setStatus(getStatusAfterExecution());
		}
	}


	public boolean shouldExecute() {
		if(file != null){
			ClearCaseStatus currentStatus = file.getStatus();
			ClearCaseStatus newStatus = getStatusAfterExecution();
			return (newStatus == null || (currentStatus != newStatus));
		}
		return true;
	}
}
